package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.PageResult;
import cn.wolfcode.crm.mapper.EmployeeMapper;
import cn.wolfcode.crm.mapper.PermissionMapper;
import cn.wolfcode.crm.qo.QueryObject;
import cn.wolfcode.crm.service.IEmployeeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EmployeeServiceImpl implements IEmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private PermissionMapper permissionMapper;

    public void delete(Long id) {
        //删除员工
        employeeMapper.deleteByPrimaryKey(id);
        //删除employee_role中间表 数据
        employeeMapper.deleteRelation(id);
    }

    public void save(Employee employee, Long[] ids) {
        //获取注册密码 进行MD5 加密(name 作为盐)
        String password = employee.getPassword();
        Md5Hash md5Hash = new Md5Hash(password, employee.getName());
        //设置加密结果
        employee.setPassword(md5Hash.toString());
        employeeMapper.insert(employee);
        //保存员工的时候也要 维护employee_role的中间表
        if (ids != null) {
            for (Long roleId : ids) {
                employeeMapper.insertRelation(employee.getId(), roleId);
            }
        }
    }

    public Employee get(Long id) {
        return employeeMapper.selectByPrimaryKey(id);
    }

    public List<Employee> listAll() {
        return employeeMapper.selectAll();
    }

    public void update(Employee employee, Long[] ids) {
        employeeMapper.updateByPrimaryKey(employee);
        //更新员工前 从employee_role的中间表删除数据
        if (ids != null) {
            employeeMapper.deleteRelation(employee.getId());
            //再根据员工新增的 角色 ids 存入employee_role中间表
            for (Long roleId : ids) {
                employeeMapper.insertRelation(employee.getId(), roleId);
            }
        }
    }

    @Override
    public PageInfo<Employee> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Employee> employees = employeeMapper.query(qo);
        return new PageInfo<Employee>(employees);
    }

    @Override
    public Employee login(String username, String password) {
        //根据用户密码查询数据库中是否存在该用户
        Employee employee = employeeMapper.selectByUsernameAndPassword(username, password);
        if (employee == null) {
            throw new RuntimeException("用户或密码不存在");
        }
        return employee;
    }

    @Override
    public List<String> getPermissionByEmployeeId(Long EmployeeId) {
        return permissionMapper.getPermissionByEmployeeId(EmployeeId);
    }

    @Override
    public void batchDelete(Long[] ids) {
        employeeMapper.batchDelete(ids);
        employeeMapper.batchDeleteRelation(ids);
    }

    @Override
    public Employee checkName(String name) {
        Employee employee = employeeMapper.selectByName(name);
        return employee;
    }

    //导出
    @Override
    public Workbook exportXls() {
        // 创建 excel 文件
        Workbook wb = new HSSFWorkbook();
        // 创建工作簿
        Sheet sheet = wb.createSheet();
        // 创建行，参数数行索引
        Row row = sheet.createRow(0);
        // 创建第一行单元格, 并设置单元格数据
        row.createCell(0).setCellValue("名称");
        row.createCell(1).setCellValue("邮箱");
        row.createCell(2).setCellValue("年龄");
        // 遍历设置每行数据
        List<Employee> employees = employeeMapper.selectAll();
        for (int i = 0; i < employees.size(); i++) {
            Employee employee = employees.get(i);
            // 每一个员工一行
            row = sheet.createRow(i + 1);
            row.createCell(0).setCellValue(employee.getName());
            row.createCell(1).setCellValue(employee.getEmail());
            row.createCell(2).setCellValue(employee.getAge());
        }
        return wb;
    }

    //导入
    @Override
    public void importXls(Workbook wb) {
        // 获取第一个工作簿
        Sheet sheet = wb.getSheetAt(0);
        // 获取最后一行索引
        int lastRowNum = sheet.getLastRowNum();
        // 遍历设置数据 (注意: lastRowNum是最后一行的索引)
        for (int i = 1; i <= lastRowNum; i++) {
            Row row = sheet.getRow(i);
            Employee employee = new Employee();
            //根据表格的数据类型  获取该行表格的每一格数据 封装成对象
            employee.setName(row.getCell(0).getStringCellValue());
            employee.setEmail(row.getCell(1).getStringCellValue());
            int age = new Double(row.getCell(2).getNumericCellValue()).intValue();
            employee.setAge(age);
            //设置默认密码为1
            employee.setPassword("1");  // 初始密码
            //调用当前类的save方法保存对象 在保存方法中已经有密码加密
            this.save(employee, null);
        }
    }

    @Override
    public List<Employee> selectEmployeeByRoleSn(String... sns) {
        return employeeMapper.selectEmployeeByRoleSn(sns);
    }
}
